{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8f4089b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import random\n",
    "from tqdm import tqdm\n",
    "import pandas as pd\n",
    "\n",
    "def detect_mandarin(text):\n",
    "    for char in text:\n",
    "        codepoint = ord(char)\n",
    "        if (\n",
    "            0x4E00 <= codepoint <= 0x9FFF or   # CJK Unified Ideographs\n",
    "            0x3400 <= codepoint <= 0x4DBF or   # CJK Unified Ideographs Extension A\n",
    "            0x20000 <= codepoint <= 0x2A6DF or # Extension B\n",
    "            0x2A700 <= codepoint <= 0x2B73F or # Extension C\n",
    "            0x2B740 <= codepoint <= 0x2B81F or # Extension D\n",
    "            0x2B820 <= codepoint <= 0x2CEAF or # Extension E\n",
    "            0x2CEB0 <= codepoint <= 0x2EBEF    # Extension F\n",
    "        ):\n",
    "            return True\n",
    "    return False\n",
    "\n",
    "transcribe = ['transcribe the audio']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "76e7a6d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('processed-cantonese-radio.json') as fopen:\n",
    "    data = json.load(fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "56ed45df",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████████████████████████████████| 1096211/1096211 [00:04<00:00, 259592.33it/s]\n"
     ]
    }
   ],
   "source": [
    "processed = []\n",
    "for d in tqdm(data):\n",
    "    new_f = d['audio_filename'].replace('-mp3/', '-mp3-16k/')\n",
    "    if not os.path.exists(new_f):\n",
    "        continue\n",
    "    \n",
    "    if not detect_mandarin(d['transcript_whisper']):\n",
    "        continue\n",
    "        \n",
    "    processed.append({\n",
    "        'question': random.choice(transcribe),\n",
    "        'answer': d['transcript_whisper'],\n",
    "        'audio_filename': new_f,\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e87f9b62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1095346"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(processed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2c2e9311",
   "metadata": {},
   "outputs": [],
   "source": [
    "cantonese = random.sample(processed, 500000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8f6397c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.DataFrame(cantonese).to_parquet('cantonese-stt.parquet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7a5241c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uploading files using Xet Storage..\n",
      "Uploading...: 100%|██████████████████████████| 123M/123M [00:13<00:00, 9.42MB/s]\n",
      "https://huggingface.co/datasets/mesolitica/Transcription-Instructions/blob/main//data/cantonese-00000-of-00001.parquet\n"
     ]
    }
   ],
   "source": [
    "!huggingface-cli upload mesolitica/Transcription-Instructions \\\n",
    "cantonese-stt.parquet /data/cantonese-00000-of-00001.parquet --repo-type=dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5bf76123",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('extra-emilia-mandarin.json') as fopen:\n",
    "    data = json.load(fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "35ac7370",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████| 868185/868185 [00:02<00:00, 295710.00it/s]\n"
     ]
    }
   ],
   "source": [
    "processed = []\n",
    "for d in tqdm(data):\n",
    "    new_f = d['audio_filename']\n",
    "    if not os.path.exists(new_f):\n",
    "        continue\n",
    "    \n",
    "    if not detect_mandarin(d['transcript_whisper']):\n",
    "        continue\n",
    "        \n",
    "    processed.append({\n",
    "        'question': random.choice(transcribe),\n",
    "        'answer': d['transcript_whisper'],\n",
    "        'audio_filename': new_f,\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "db9ef0e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "865405"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(processed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c9cc185f",
   "metadata": {},
   "outputs": [],
   "source": [
    "mandarin = random.sample(processed, 500000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "67ac6594",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.DataFrame(mandarin).to_parquet('mandarin-stt.parquet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d31c8935",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uploading files using Xet Storage..\n",
      "Uploading...: 100%|████████████████████████| 56.8M/56.8M [00:10<00:00, 5.22MB/s]\n",
      "https://huggingface.co/datasets/mesolitica/Transcription-Instructions/blob/main//data/mandarin-00000-of-00001.parquet\n"
     ]
    }
   ],
   "source": [
    "!huggingface-cli upload mesolitica/Transcription-Instructions \\\n",
    "mandarin-stt.parquet /data/mandarin-00000-of-00001.parquet --repo-type=dataset"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
